home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 9 / CDACTUAL9.iso / share / Dos / VARIOS / pascal / SWAG9605.DDD / 0143_Helpful Procedures and Functions.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-05-31  |  5.8 KB  |  341 lines

  1. unit utils;
  2. {$g+,d+}
  3.  
  4. INTERFACE
  5.  
  6. const
  7.   c_warning=$01;
  8.   c_error=$02;
  9.   c_display=$fe;
  10.   c_fatal=$ff;
  11.  
  12. var
  13.   timer:longint absolute $0040:$006c;
  14.  
  15. procedure keep(const code:byte);
  16. procedure getint(const num:byte;var p:pointer);
  17. procedure setint(const num:byte;const p:pointer);
  18. procedure asmcall(const p:pointer);
  19. function  fex(const fn:string):boolean;
  20. function  fsearch(const namep,pathp:string):string;
  21. function  percent(const a,b:longint):longint;
  22. function  hexbyte(const b:byte):string;
  23. function  hexword(const w:word):string;
  24. function  hexlong(const ww:longint):string;
  25. function  fsize(const fn:string):longint;
  26. function  fsize2(var f:file):longint;
  27. function  smartdrver:integer;
  28. procedure starttime;
  29. function  stoptime:longint;
  30. procedure error(s:string;x,y,mode:byte);
  31. function  small(a,b:word):word;
  32. function  large(a,b:word):word;
  33. function  fdel(fn:string):boolean;
  34. function  fren(n1,n2:string):boolean;
  35. function  legalname(const fn:string):boolean;
  36. function  buildstr(const ch:char;const num:byte):string;
  37. procedure flush_cache;
  38.  
  39. IMPLEMENTATION
  40.  
  41. uses crt;
  42.  
  43. var
  44.   oldtime:longint;
  45.  
  46. procedure keep(const code:byte); assembler;
  47. asm
  48.   mov ax,prefixseg
  49.   mov es,ax
  50.   mov dx,word ptr es:2
  51.   sub dx,ax
  52.   mov al,code
  53.   mov ah,31h
  54.   int 21h
  55. end;
  56.  
  57. procedure getint(const num:byte;var p:pointer); assembler;
  58. asm
  59.   push ds
  60.   xor ax,ax
  61.   mov ds,ax
  62.   mov al,num
  63.   mov si,ax
  64.   shl si,2
  65.   les di,p
  66.   db 66h; movsw
  67.   pop ds
  68. end;
  69.  
  70. procedure setint(const num:byte;const p:pointer); assembler;
  71. asm
  72.   cli
  73.   xor ax,ax
  74.   mov es,ax
  75.   mov al,num
  76.   mov di,ax
  77.   shl di,2
  78.   mov ax,word ptr [p]
  79.   mov es:[di],ax
  80.   mov ax,word ptr [p+2]
  81.   mov es:[di+2],ax
  82.   sti
  83. end;
  84.  
  85. procedure asmcall(const p:pointer);assembler;
  86. asm
  87.   call p
  88. end;
  89.  
  90. function fsearch(const namep,pathp:string):string; assembler;
  91. asm
  92.   push ds
  93.   cld
  94.   lds si,pathp
  95.   lodsb
  96.   mov bl,al
  97.   xor bh,bh
  98.   add bx,si
  99.   les di,@result
  100.   inc di
  101. @@1:
  102.   push si
  103.   push ds
  104.   lds si,namep
  105.   lodsb
  106.   mov cl,al
  107.   xor ch,ch
  108.   rep movsb
  109.   xor al,al
  110.   stosb
  111.   dec di
  112.   mov ax,4300h
  113.   lds dx,@result
  114.   inc dx
  115.   int 21h
  116.   pop ds
  117.   pop si
  118.   jc @@2
  119.   test cx,18h
  120.   je @@5
  121. @@2:
  122.   les di,@result
  123.   inc di
  124.   cmp si,bx
  125.   je @@5
  126.   xor ax,ax
  127. @@3:
  128.   lodsb
  129.   cmp al,';'
  130.   je @@4
  131.   stosb
  132.   mov ah,al
  133.   cmp si,bx
  134.   jne @@3
  135. @@4:
  136.   cmp ah,':'
  137.   je @@1
  138.   cmp ah,'\'
  139.   je @@1
  140.   mov al,'\'
  141.   stosb
  142.   jmp @@1
  143. @@5:
  144.   mov ax,di
  145.   les di,@result
  146.   sub ax,di
  147.   dec ax
  148.   stosb
  149. @@6:
  150.   pop ds
  151. end;
  152.  
  153. function fex(const fn:string):boolean;
  154. begin
  155.   fex:=(fsearch(fn,'')<>'');
  156. end;
  157.  
  158. function percent(const a,b:longint):longint;
  159. begin
  160.   percent:=round(a/b*100);
  161. end;
  162.  
  163. function hexbyte(const b:byte):string;
  164. const hex:array[0..16]of char='0123456789abcdef';
  165. begin
  166.   hexbyte:=hex[b shr 4]+hex[b and $f];
  167. end;
  168.  
  169. function hexword(const w:word):string;
  170. begin
  171.   hexword:=hexbyte(hi(w))+hexbyte(lo(w));
  172. end;
  173.  
  174. function hexlong(const ww:longint):string;
  175. var w:array[1..2]of word absolute ww;
  176. begin
  177.   hexlong:=hexword(w[2])+hexword(w[1]);
  178. end;
  179.  
  180. function fsize(const fn:string):longint;
  181. var f:file;
  182. begin
  183.   fsize:=-1;
  184.   if not(fex(fn))then exit;
  185.   assign(f,fn);
  186.   {$i-} reset(f,1); {$i+}
  187.   if(ioresult<>0)then exit;
  188.   fsize:=filesize(f);
  189.   close(f);
  190. end;
  191.  
  192. function fsize2(var f:file):longint;
  193. begin
  194.   fsize2:=-1;
  195.   {$i-} close(f); {$i+} if(ioresult<>0)then ;
  196.   {$i-} reset(f,1); {$i+}
  197.   if(ioresult<>0)then exit;
  198.   fsize2:=filesize(f);
  199.   close(f);
  200. end;
  201.  
  202. function smartdrver:integer; assembler;
  203. asm
  204.   xor bx,bx
  205.   xor cx,cx
  206.   xor dx,dx
  207.   mov ax,04a10h
  208.   int 02fh
  209.   jc @@error
  210.   cmp ax,0babeh
  211.   jne @@error
  212.   mov ax,bp
  213.   jmp @@exit
  214.   @@error:
  215.     mov ax,1
  216.     neg ax
  217.   @@exit:
  218. end;
  219.  
  220. procedure starttime;
  221. begin
  222.   oldtime:=timer;
  223. end;
  224.  
  225. function stoptime:longint;
  226. var tmp:longint;
  227. begin
  228.   tmp:=timer;
  229.   stoptime:=(tmp-oldtime);
  230. end;
  231.  
  232. procedure error(s:string;x,y,mode:byte);
  233. var
  234.   fore:string;
  235.   old:byte;
  236. begin
  237.   old:=textattr;
  238.   gotoxy(x,y);
  239.   case mode of
  240.     c_warning:begin fore:='warning: '; textcolor(darkgray); end;
  241.     c_error:  begin fore:='error: '; textcolor(lightred); end;
  242.     c_fatal:  begin fore:='fatal: '; textcolor(red); end;
  243.     c_display:begin fore:=''; textcolor(white); end;
  244.   end;
  245.   write(fore,s);
  246.   textattr:=old;
  247.   if(mode in [c_fatal,c_display])then halt(1);
  248. end;
  249.  
  250. function small(a,b:word):word; assembler;
  251. asm
  252.   mov ax,a
  253.   mov bx,b
  254.   cmp ax,bx
  255.   jbe  @@exit
  256.   mov ax,bx
  257.   @@exit:
  258. end;
  259.  
  260. function large(a,b:word):word; assembler;
  261. asm
  262.   mov ax,a
  263.   mov bx,b
  264.   cmp ax,bx
  265.   jae  @@exit
  266.   mov ax,bx
  267.   @@exit:
  268. end;
  269.  
  270. function setfattr(var filep:file;const attr:word):word; assembler;
  271. asm
  272.   push ds
  273.   lds dx,filep
  274.   add dx,48
  275.   mov cx,attr
  276.   mov ax,4301h
  277.   int 21h
  278.   pop ds
  279.   jc  @@exit
  280.   xor ax,ax
  281. @@exit:
  282. end;
  283.  
  284. function legalname(const fn:string):boolean;
  285. var f:file;
  286. begin
  287.   legalname:=true;
  288.   if(fex(fn))then exit;
  289.   assign(f,fn);
  290.   setfattr(f,0);
  291.   {$i-} rewrite(f,1); {$i+}
  292.   if(ioresult<>0)then legalname:=false;
  293.   {$i-} erase(f); {$i+} if(ioresult<>0)then ;
  294. end;
  295.  
  296. function fdel(fn:string):boolean;
  297. var f:file;
  298. begin
  299.   fdel:=false;
  300.   if not(fex(fn))then exit;
  301.   assign(f,fn);
  302.   if(setfattr(f,0)<>0)then exit;
  303.   {$i-} erase(f); {$i+} if(ioresult<>0)then exit;
  304.   fdel:=true;
  305. end;
  306.  
  307. function fren(n1,n2:string):boolean;
  308. var f:file;
  309. begin
  310.   fren:=false;
  311.   if not(fex(n1))or(fex(n2))then exit;
  312.   assign(f,n1);
  313.   {$i-} rename(f,n2); {$i+} if(ioresult<>0)then exit;
  314.   fren:=true;
  315. end;
  316.  
  317. function buildstr(const ch:char;const num:byte):string; assembler;
  318. asm
  319.   xor ch,ch
  320.   mov al,[num]
  321.   mov cl,al
  322.   les di,@result
  323.   stosb
  324.   jcxz @@exit
  325.   mov al,[&ch]
  326.   mov ah,al
  327.   shr cl,1
  328.   rep stosw
  329.   adc cl,cl
  330.   rep stosb
  331.   @@exit:
  332. end;
  333.  
  334. procedure flush_cache; assembler;
  335. asm
  336.   mov ax,04a10h
  337.   mov bx,1
  338.   int 02fh
  339. end;
  340.  
  341. end.